package com.vgooo.client.mobile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.vgooo.bean.ProdDetail;
import com.vgooo.bean.ProdInfo;
import com.vgooo.client.Main;
import com.vgooo.client.R;
import com.vgooo.client.VGActivity;
import com.vgooo.client.ViewLayout;
import com.vgooo.client.ui.VGViewPager;
import com.vgooo.client.ui.ViewPagerAdapter;
import com.vgooo.client.vchannel.PingActivity;
import com.vgooo.client.vchannel.PingDetail;
import com.vgooo.util.DebugTools;
import com.vgooo.util.GSonTools;
import com.vgooo.util.HttpUtils;
import com.vgooo.util.Utils;

public class MobileDetailActivity extends VGActivity {

	String className = getClass().getSimpleName();// 类名，用于打印调试信息

	private ProdInfo prodInfo;// 热门推荐手机信息
	private ProdDetail prodDetail;// 手机详情

	private TextView pingCount ;

	// 请求参数
	private String prod_html;
	private String brand_html;


	private PingDetail pingDetail;


	private String link = "";


	private int standarWidth = 0;
	
	private com.vgooo.client.ui.VGViewPager  viewPager ; 
//	private ViewPager  viewPager ; 
	
	private View titleView ;
	
	private WebView webView ;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		detailVeiw = inflater.inflate(R.layout.mobile_detail_v2, null);

		setContentView(detailVeiw);
		detailVeiw.setVisibility(View.INVISIBLE);
		initView();

		needNewProgress = true;
		standarWidth = Utils.getSceenWidth(context);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// setContentView(detailVeiw);
		// 友盟统计功能，打开页面
		MobclickAgent.onResume(this);
		String link = Main.moblieDetailIntent.getStringExtra("link");
		if(null==link||"".equals(link)||link.trim().length()<1){
			ViewLayout.setViewBack();
			return ;
		}
		DebugTools.log(className, link.toString());
		if (this.link.equals(link)) {
			detailVeiw.setVisibility(View.VISIBLE);
			// 显示产品名称
			return;
		} else {

			detailVeiw = inflater.inflate(R.layout.mobile_detail_v2, null);

			setContentView(detailVeiw);
			detailVeiw.setVisibility(View.INVISIBLE);

			initView();
			addListener();

			this.link = link;
			init();
		}
		
		
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		// 友盟统计功能，页面关闭
		MobclickAgent.onPause(this);
		Utils.hideSoftInput(detailVeiw);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		
		
		viewPager = (VGViewPager) findViewById(R.id.mobile_detail_view_pager);
		

		// 更多详情
		webView = new WebView(context);
		// 显示产品更多介绍
		webView.setVisibility(WebView.VISIBLE);		
		webView.setInitialScale(150);
		webView.addJavascriptInterface(this, "android");
		WebSettings setting = webView.getSettings();
		setting.setUseWideViewPort(false);
		setting.setSupportZoom(false);
		setting.setDefaultTextEncodingName("UTF-8");// 设置编码
		setting.setPluginsEnabled(true);// 是否支持插件
		setting.setJavaScriptEnabled(true);// 设置是否支持javaScript
		// 评论
		pingDetail = new PingDetail(cparent, PingActivity.TYPE_PROD);
		pingDetail.initView(detailVeiw.findViewById(R.id.mobile_detail_ping) );
				
		pingCount = (TextView) findViewById(R.id.ping_bottom_layout_button);

	}
	@Override
	public void setupView() {
		// TODO Auto-generated method stub
		// setContentView(detailVeiw);
		progress.start() ;
		List<View> views = new ArrayList<View>();
		views.add(webView);
		viewPager.setAdapter(new ViewPagerAdapter(viewPager,views,pingCount,prodDetail.prod_ping,pingDetail,null));

		viewPager.setCurrentItem(1);


		webView.loadUrl("");
		webView.loadDataWithBaseURL(null, prodDetail.contents, "text/html",
				"UTF-8", null);
		titleView =  inflater.inflate(R.layout.layout_mobile_title, null);
		if(null!=prodDetail.prod_other_name1&&!"".equals(prodDetail.prod_other_name1)){
			((TextView)titleView.findViewById(R.id.mobile_detail_title)).setText(prodDetail.brand_name+" ("+prodDetail.prod_other_name1+")");
		}else{
			((TextView)titleView.findViewById(R.id.mobile_detail_title)).setText(prodDetail.brand_name);
		}
		pingDetail.setupView(prodDetail.prod_ping, prodDetail.prod_id,titleView);

		detailVeiw.setVisibility(View.VISIBLE);
		
		

	}
	@Override
	public void addListener() {
		// TODO Auto-generated method stub
		webView.setWebChromeClient(new WebChromeClient(){

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				if(newProgress>=50){
					progress.cancel();
				}
				super.onProgressChanged(view, newProgress);
			}
			
		});
	}

	@Override
	public void getData() {
		try {
			// TODO Auto-generated method stub
			String urlPath = HttpUtils.BASE_URI + link;
			String jsonString = HttpUtils.getJsonString(urlPath.toString());
			DebugTools.log(className, "jsonString = " + jsonString);
			Map<String, Object> prods = GSonTools.getObjectMaps(jsonString);
			DebugTools.log(className, "prods = " + prods);
			Map<String, Object> brand = (Map<String, Object>) prods
					.get("brand");
			prodDetail = new ProdDetail();
			prodDetail.brand_name = (String) brand.get("brand_name");
			prodDetail.brand_html = (String) brand.get("brand_html");

			Map<String, Object> prod = (Map<String, Object>) prods.get("prod");
			prodDetail.prod_image = (String) prod.get("prod_image");
			prodDetail.prod_id = (String) prod.get("prod_id");
			prodDetail.prod_html = (String) prod.get("prod_html");
			prodDetail.brand_name += " " + (String) prod.get("prod_name");
			
			prodDetail.prod_other_name1 = (String) prod.get("prod_other_name1") ;

		
			
			new Thread(){
				public void run() {
					String url = null ;
					try {
						url = HttpUtils.BASE_URI + "prodcontentmakethumb?pid="
								+ prodDetail.prod_id + "&sw=" + standarWidth;
						HttpUtils.getJsonString(url);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally{
						url = null ;
					}
				};
			}.start();
			urlPath = HttpUtils.BASE_URI + "prodgetcontentdesc2?pid="
					+ prodDetail.prod_id + "&sw=" + standarWidth + "&dp="
					+ Utils.getSceenWidth(context);Log.i("aaaaaaa", urlPath);
			String jsonString2 = HttpUtils.getJsonString(urlPath);
			urlPath = null ;
			prodDetail.contents = jsonString2;

			prodDetail.prod_ping = (String) prods.get("prod_ping");


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0&&null!=pingDetail) {
			if(pingDetail.isEdit){
				pingDetail.chanageEdit(false);
				return true ;
			}
		}
		return super.onKeyDown(keyCode, event);
	}


	@Override
	public void doClick(View v) {
		// TODO Auto-generated method stub

		// String parent_id = null ;
		switch (v.getId()) {

		default:
			if (pingDetail != null) {
				pingDetail.doClick(v);
			} else {
				DebugTools.log(className,
						"doClick() 未定义控件单击后的事件......" + v.getId());
			}
			break;
		}
	}

	
	private static void log(String info){
		Log.i("MobileDetailActivity", info);
	}
}
